package com.study.zzedu.ui.discussdetail;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.study.zzedu.R;

import com.study.zzedu.adapter.DiscussDetailAdapter;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.entity.DiscussDetailItem;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
//课程讨论详情页
public class DiscussDetailActivity extends BaseMvpActivity<DiscussDetailContract.IView, DiscussDetailPresenter> implements DiscussDetailContract.IView {

    private List<DiscussDetailItem> dataArray = new ArrayList<>();
    private RecyclerView recycler;
    private EditText edit_view;
    @Override
    protected int getLayoutID() {
        return R.layout.activity_discuss_detail_info;
    }

    @Override
    protected DiscussDetailPresenter createPresenter() {
        return new DiscussDetailPresenter();
    }

    @Override
    protected DiscussDetailContract.IView createView() {
        return this;
    }


    @Override
    protected void initView() {
        //加载数据,自定义函数在函数中加载需要显示的数据
        recycler = (RecyclerView) findViewById(R.id.rv_discuss_detail_list);

        edit_view = (EditText)findViewById(R.id.edit_view);
    }
    DiscussDetailAdapter adapter;
    private String pageNum = "1";
    private String pageSize="50";
    @Override
    protected void initData() {
        LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
        recycler.setLayoutManager(layoutmanager);

        adapter= new DiscussDetailAdapter(dataArray);//m是集合，上面应该定义一个集合
        recycler.addItemDecoration(new SpacesItemDecoration(30));
        recycler.setAdapter(adapter);
        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
                int totalItemCount = recyclerView.getAdapter().getItemCount();
                int lastVisibleItemPosition = lm.findLastVisibleItemPosition();
                int visibleItemCount = recyclerView.getChildCount();

                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItemPosition == totalItemCount - 1
                        && visibleItemCount > 0) {
                    //加载更多
                    //getPresenter().getDiscussDetail(DiscussDetailActivity.this,AppConstant.contentID,String.valueOf(pageNum++),pageSize);
                }
            }
        });
        if(TextUtils.isEmpty(AppConstant.contentID)){
            ToastUtil.showShortToast("帖子异常");
            return;
        }
        getPresenter().getDiscussDetail(DiscussDetailActivity.this,AppConstant.contentID,pageNum,pageSize);
    }

    @Override
    protected void initListener() {
        findViewById(R.id.rl_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        findViewById(R.id.right_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(edit_view.getText().toString())) {
                    ToastUtil.showShortToast("发送的消息不能为空！");
                    return;
                }
                getPresenter().sendData(DiscussDetailActivity.this,AppConstant.contentID, edit_view.getText().toString());
                edit_view.setText("");
            }
        });

    }

    @Override
    public void getData(String data) {

    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    @Override
    public void getMsgReply(String name) {
        getPresenter().getDiscussDetail(DiscussDetailActivity.this,AppConstant.contentID,pageNum,pageSize);
    }

    @Override
    public void getDataList(String data) {
        try {
            dataArray.clear();
            JSONObject jObject = new JSONObject(data);
            JSONArray jsonArray = jObject.getJSONArray("data");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject job = jsonArray.getJSONObject(i);
                dataArray.add(new DiscussDetailItem(
                        job.getString("id"), job.getString("courseDiscussionId"),
                        job.getString("content"), job.getInt("floor"),
                        job.getString("pubdate"), job.getString("replierName"),
                        job.getBoolean("byPoster")));
            }

            if (!(jsonArray.length() > 0)) {
                ToastUtil.showShortToast("无数据");
                return;
            }
            if(adapter != null)
                adapter.notifyDataSetChanged();

        } catch (Exception e) {
            LogUtil.e("Exception", e.getMessage());
        }
    }
}